﻿using System;
using System.Drawing;
using Autodesk.Revit.DB;

namespace YRevitJig
{
	internal class Utility
	{
		public static bool GetGraphicsArcInfo(System.Drawing.Point dpt1, System.Drawing.Point dpt2, System.Drawing.Point dptOnArc, ref RectangleF rect, ref double dAngS, ref double dAngSweep)
		{
			try
			{
				Utility.Circle2D circle2D = Utility.Circle2D.CreateCircle2D(dpt1, dpt2, dptOnArc);
				XYZ xyz = new XYZ((double)dpt1.X, (double)dpt1.Y, 0.0);
				XYZ xyz2 = new XYZ((double)dpt2.X, (double)dpt2.Y, 0.0);
				XYZ xyz3 = new XYZ((double)circle2D.circlecentre.X, (double)circle2D.circlecentre.Y, 0.0);
				XYZ xyz4 = new XYZ((double)dptOnArc.X, (double)dptOnArc.Y, 0.0);
				XYZ xyz5 = -XYZ.BasisZ;
				rect = new RectangleF((float)(xyz3.X - circle2D.raduis), (float)(xyz3.Y - circle2D.raduis), (float)(2.0 * circle2D.raduis), (float)(2.0 * circle2D.raduis));
				XYZ xyz6 = xyz2 - xyz;
				XYZ xyz7 = xyz4 - xyz;
				XYZ xyz8 = xyz;
				XYZ xyz9 = xyz2;
				bool flag = !xyz6.CrossProduct(xyz7).Normalize().IsAlmostEqualTo(xyz5);
				if (flag)
				{
					xyz8 = xyz2;
					xyz9 = xyz;
				}
				dAngS = XYZ.BasisX.AngleOnPlaneTo(xyz8 - xyz3, -xyz5);
				dAngSweep = (xyz8 - xyz3).AngleOnPlaneTo(xyz9 - xyz3, -xyz5);
				dAngS *= 57.295779513082323;
				dAngSweep *= 57.295779513082323;
			}
			catch (Exception ex)
			{
				return false;
			}
			return true;
		}

		private class Circle2D
		{
			public static Utility.Circle2D CreateCircle2D(System.Drawing.Point point1, System.Drawing.Point point2, System.Drawing.Point point3)
			{
				Utility.Circle2D circle2D = new Utility.Circle2D();
				double num = (double)((point2.X * point2.X + point2.Y * point2.Y - (point1.X * point1.X + point1.Y * point1.Y)) * (2 * (point3.Y - point1.Y)) - (point3.X * point3.X + point3.Y * point3.Y - (point1.X * point1.X + point1.Y * point1.Y)) * (2 * (point2.Y - point1.Y)));
				double num2 = (double)(2 * (point2.X - point1.X) * (point3.X * point3.X + point3.Y * point3.Y - (point1.X * point1.X + point1.Y * point1.Y)) - 2 * (point3.X - point1.X) * (point2.X * point2.X + point2.Y * point2.Y - (point1.X * point1.X + point1.Y * point1.Y)));
				double num3 = (double)(4 * ((point2.X - point1.X) * (point3.Y - point1.Y) - (point3.X - point1.X) * (point2.Y - point1.Y)));
				circle2D.circlecentre.X = (int)(num / num3);
				circle2D.circlecentre.Y = (int)(num2 / num3);
				circle2D.raduis = Math.Sqrt((double)((point1.X - circle2D.circlecentre.X) * (point1.X - circle2D.circlecentre.X) + (point1.Y - circle2D.circlecentre.Y) * (point1.Y - circle2D.circlecentre.Y)));
				return circle2D;
			}

			public System.Drawing.Point circlecentre;

			public double raduis;
		}
	}
}
